clear; 
close all;

sw.plotAna = 1;
sw.plotCom = 1;

opt.maxPeaks = 40;
opt.expandRatio = 1;
opt.freqShift = 0;
opt.winType = 'hann';

% [x,fs] = audioread('../wavfiles/wrenpn.wav');
% [x,fs] = audioread('../wavfiles/peaches_16.wav');
% [x,fs] = audioread('../wavfiles/mymom_16.wav');
 [x,fs] = audioread('../wavfiles/draw_16.wav');

x = x(:, 1);    % mono/left channel

%% SET PARAMETERS
nx = length(x);                             % sample number of signal
samPerFra = 512;                            % samples per frame
hopSize = 0.5;                              % hop samples / sample per frame
hopSamples = floor((samPerFra+1) * hopSize);
nFrames = ceil((nx-samPerFra)/hopSamples)+1;
nFFT = 2^(1+floor(log2(5*samPerFra+1))); 
                % FFT length, at least a factor of 5 zero-padding

maxPeaks = opt.maxPeaks;
fRatio = 2^(opt.freqShift/12); 
tRatio = opt.expandRatio;

%% ANALYSIS
% zero-padding
xz = zeros((nFrames-1)*hopSamples + samPerFra, 1);
xz(1:nx) = x;

[amps, freqs, phases] = SinAnalysis(xz, nFrames, samPerFra, nFFT, hopSamples, opt.winType, maxPeaks);

%% SYNTHESIS
% audio modification
freqs(:) = min(fRatio*freqs(:), pi);
samPerFra = samPerFra*tRatio;
hopSamples = hopSamples*tRatio;

tic;
y = SinSynth(amps, freqs, phases, nFrames, samPerFra, hopSamples, opt.winType);
tim = toc;

fprintf("syhthesis time:%g\n", tim);

% original signal in left channel, resynthsized signal in right channel
sound([xz, y], fs);

%% PLOT RESULTS
FigOutput(sw, opt, fs, freqs, xz, y);
